#include "IPName.h"
#include "ui_IPName.h"
#include "ConfigManager.h"

#include <QMessageBox>
#include <QFile>

IPName::IPName(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::IPName)
{
    m_ui->setupUi(this);
}

IPName::~IPName()
{
    delete m_ui;
}

bool IPName::Initialize()
{
	setWindowIcon(QIcon("../img/icon.png"));
	Initwidget();
	return true;
}

void IPName::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

bool IPName::AlreadyExist(QString ipName)
{
	bool found = false;
	for (int i = 0; i < m_ui->listWidget->count() && !found; i++)
		found = (m_ui->listWidget->item(i)->text() == ipName);
	return found;
}

void IPName::Initwidget()
{
	m_ui->listWidget->clear();
	for (int i = 0; i < CONFIG_MANAGER->GetIPNamesCount(); i++)
		m_ui->listWidget->addItem(CONFIG_MANAGER->GetIPName(i, true));
}

void IPName::on_btn_add_clicked()
{
	QString ip = m_ui->plainTextEdit_ip->toPlainText();
	QString name = m_ui->plainTextEdit_name->toPlainText();
	if (ip.isEmpty() || name.isEmpty())
		QMessageBox::warning(this, "warning", "input data");
	else
	{
		//addList..
		if ((CONFIG_MANAGER->GetLocalAddress().toString() != ip ||
			CONFIG_MANAGER->GetName() != name))
		{
			if (!AlreadyExist(ip + ':' + name))
			{
				m_ui->listWidget->addItem(ip + ':' + name);
				m_ui->plainTextEdit_ip->clear();
				m_ui->plainTextEdit_name->clear();
			}
			else
				QMessageBox::warning(this, "warning", "dup user");
		}
		else
			QMessageBox::warning(this, "warning", "cannot set my info");
	}
}

void IPName::on_btn_del_clicked()
{
	if (m_ui->listWidget->count())
		m_ui->listWidget->takeItem(m_ui->listWidget->currentRow());
}

void IPName::on_btn_ok_clicked()
{
	if (!QFile::copy("../conf/ip_name.txt", "../conf/ip_name_backup.txt"))
		qDebug() << "ip_name.txt backup create failed";
	bool error = false;
	FILE *fp = fopen("../conf/ip_name.txt", "wt");
	if (fp != NULL)
	{
		for (int i = 0; i < m_ui->listWidget->count() && !error; i++)
			error = (fputs(QString(m_ui->listWidget->item(i)->text() + '\n').toAscii().data(), fp) < 0);
		fclose(fp);
	}
	else
		error = true;
	if (error)
		QFile::rename("../conf/ip_name_backup.txt", "../conf/ip_name.txt");
	else
		QFile::remove("../conf/ip_name_backup.txt");

	if (CONFIG_MANAGER->IPNameConfigInitialize())
		qDebug() << "config changed";
	else
		qDebug() << "config change failed";

	Initwidget();
	hide();
}

void IPName::on_btn_addmsg_clicked()
{
	if (m_ui->listWidget->count())
	{
		QString ipName = m_ui->listWidget->currentItem()->text();
		QString name = ipName.right(ipName.length() - ipName.indexOf(':') - 1);
		emit sigAddSendList('@' + name);
	}
}

void IPName::on_listWidget_itemDoubleClicked(QListWidgetItem* /*item*/)
{

}

void IPName::on_listWidget_itemClicked(QListWidgetItem* item)
{
	QString ipName = item->text();
	if (!ipName.isEmpty())
	{
		int colonIdx = ipName.indexOf(':');
		if (colonIdx != -1)
		{
			QString ip = ipName.mid(0, colonIdx);
			QString name = ipName.right(ipName.length() - colonIdx - 1);
			m_ui->plainTextEdit_ip->setPlainText(ip);
			m_ui->plainTextEdit_name->setPlainText(name);
		}
	}
}

void IPName::on_listWidget_itemChanged(QListWidgetItem* /*item*/)
{

}
